package serializer

import (
	"QImages/entity"
	"QImages/global"
	"QImages/utils"
)

type Collection struct {
	UserID    uint   `json:"user_id"`
	ImageID   uint   `json:"image_id"`
	CreatedAt string `json:"created_at"`
	Url       string `json:"url"`
	Desc      string `json:"desc"`
	Title     string `json:"title"`
	Label     string `json:"label"`
}

//BuildCollection 序列化收藏
func BuildCollection(c *entity.Collection) (res Collection) {
	res.UserID = c.UserID
	res.ImageID = c.ImageID
	image, err := entity.FindImageById(res.ImageID)
	if err != nil {
		return
	}
	res.CreatedAt = utils.FormatDatetime(image.CreatedAt)
	res.Url = global.Config.PhotoPrefix + image.Url
	res.Desc = image.Desc
	res.Title = image.Title
	res.Label = image.Label
	return
}

//BuildCollections 批量序列化收藏
func BuildCollections(cs []entity.Collection) (res []Collection) {
	for _, c := range cs {
		res = append(res, BuildCollection(&c))
	}
	if res == nil {
		res = make([]Collection, 0)
	}
	return
}
